/**
 * Created by Loriling on 2015/6/16.
 */
define(function(){
    var PAGE_ID = "UOBSCH";
    var PROPERTY_ABANDON_OB = "temp.abandon_ob";
    var Outbound = function(proj){
        this.project = proj;
        this.instance;
        this.obInfo = {
            objectiveTypeOblistType : 0,
            autoNext : false,
            onlyReturn : false,
            currentOBListGuid : "",
            currentOBObjectiveGuid : "",
            currentOBObjectiveType : "",
            //set value when open ob detail page : for OBWindow 回拨时需要
            currentContactPhone : "",
            currentCustomerNotifyGuid : "",
            clear : function(){
                $F.log("Outbound.obInfo", "Do some clear");
            }
        };
    };
    Outbound.prototype = {
        init : function(){
            $F.log('Outbound - init', 'init outbound.');
        },
        open : function(pg){
            const ob = this;
            const proj = this.project;
            const nav = proj.getNavs();
            for (var i = 0; i < nav.length; i++) {
                if ($(nav[i]).data("addinId") === $CONST.ModuleName.ADDIN_OUTBOUND) {
                    proj.tabReopenByIndex(i);
                    ob.instance.doCmd("P-55");
                    return true;
                }
            }
            //没有发现外拨tab，则创建一个

            var uobsch = proj.getParam(PAGE_ID);
            if (!uobsch){
                //$F.warn(i18n.sys.paramerr + ": " + PAGE_ID);
                return false;
            }
            var instance = $D.createInstance(proj, uobsch, '', '', undefined, true, 'DYN', proj.veSupport);
            if (!instance){
                return false;
            }
            ob.instance = instance;
            instance.propertyListener($CONST.ModuleName.ADDIN_OUTBOUND, function (pName, pValue) {
                pName = pName.toUpperCase();
                if (pName === PROPERTY_ABANDON_OB) {
                    if (pValue == "1") {
                        $E.frameUnLock();
                        proj.tabClose(instance.id);
                        $F.fireValue(instance, PROPERTY_ABANDON_OB, "0");
                    }
                }
            });
            proj.addModuleInstance($CONST.ModuleName.ADDIN_OUTBOUND, instance);

            var tab = proj.tabCreate($CONST.ModuleName.ADDIN_OUTBOUND, $F.lang('outbound.outbound'), false, false);
            tab.data("instance", ob.instance);
            tab.data("addinId", $CONST.ModuleName.ADDIN_OUTBOUND);
            $D.live(ob.instance, tab.data("container"));
            ob.instance.doCmd("P-55");
            return true;
        }
    };

    var Util = function(proj){
        this.queryAPCallSql;
        this.queryPersonalCallSql;
        this.extTBName;

        this.init = function(){
            var queryAPCallSql = proj.getParam("OBSQLP");
            if(!$F.isEmpty(queryAPCallSql)){
                queryAPCallSql = queryAPCallSql.replace(/<rolegroup_id>/gi, proj.groupId)
                	.replace(/<objectivetype_id>/gi, $E.loginedObjectiveTypeId)
                	.replace(/<staff_id>/gi, $E.staff.id);
            }
            if (queryAPCallSql)
            	$F.log("Outbound.util", "OBList apcall query:" + queryAPCallSql);
            this.queryAPCallSql = queryAPCallSql;

            var queryPersonalCallSql = proj.getParam("PERSQL");
            if(!$F.isEmpty(queryPersonalCallSql)){
                queryPersonalCallSql = queryPersonalCallSql.replace(/<rolegroup_id>/gi, proj.groupId)
                	.replace(/<objectivetype_id>/gi, $E.loginedObjectiveTypeId)
                	.replace(/<staff_id>/gi, $E.staff.id);
            }
            if (queryPersonalCallSql)
            	$F.log("Outbound.util", "OBList personal query:" + queryPersonalCallSql);
            this.queryPersonalCallSql = queryPersonalCallSql;

        };
        this.init();

        this.getConfigedAPCall = function () {
            //改成读取全局sql的方式
            var data = $F.ajaxSync("CacheDoPubSql2",{
				token: proj.token,
				dbPool: '',
				sqls: [{
					pubKey: 'PRIOAP', //全局sql的key
					page: 0,
					maxRow: proj.pubSqlMaxRows,
					para: {// 参数 key: value
						rolegroup_id: proj.groupId,
						staff_id: $E.staff.id
					}
				}]
			}, proj.digest);
            if (data && data.value && data.value[0].count > 0) {
            	if ($.isArray(data.value[0].fields)) {
            		return data.value[0].fields[0][0];
            	} else {
            		// 如果全局变量设置了主键，则返回的是一个以主键为key的map
            		var keys = Object.keys(data.value[0].fields);
            		if (keys.length > 0) {
            			return keys[0];
            		}
            	}
            }
        };

        this.queryByGuid = function(oblistGuid){
            var ret = proj.commonQuerySync("ECore.Outbound.queryOblistByGuid", [$E.staff.epidSql, oblistGuid]);
            if(!$F.isNull(ret) && ret.value[0].count > 0) {
                return ret.value[0].fields[0];
            }
            return undefined;
        };
        
        this.queryTaskByGuid = function(taskGuid){
        	var ret = proj.commonQuerySync("ECore.EliteTask.selectDetailByGuid", [$E.staff.epidSql, taskGuid]);
        	if(!$F.isNull(ret) && ret.value[0].count > 0) {
        		return $F.makeJsonBean(ret.value[0]);
        	}
        	return undefined;
        };

        this.queryObjectiveByGuid = function(objectiveGuid){
            var ret = proj.commonQuerySync("ECore.Outbound.queryObjectiveByGuid", [$E.staff.epidSql, objectiveGuid]);
            if(!$F.isNull(ret) && ret.value[0].count > 0) {
                return ret.value[0].fields[0];
            }
            return undefined;
        };

        this.getOBList = function () {

        };
        this.getAPCallOBList = function () {

        };
        this.getGroupOBList = function () {

        };
        this.getPersonalOBList = function () {
        };
        
        this.updateOblistAppCall = function (obList) {
        	var bd = new BoundData("OBList_AppCall" + $E.staff.epidSql);
        	bd.KeyInfoEdit("OBList_GUID");
            bd.setModifyInfo("MODIFIEDDATE", "MODIFIEDBY", $E.staff.id);
            bd.setValue('OBList_GUID', obList.getValue('OBList_GUID'));
            bd.setValue('Objective_GUID', obList.getValue('Objective_GUID'));
            bd.setValue('objectivetype_id', obList.getValue('objectivetype_id'));
            bd.setValue('expectstarttime', obList.getValue('expectstarttime'));
            bd.setValue('obstatus', obList.getValue('obstatus'));
            bd.setValue('handleby_id', obList.getValue('handleby_id'));
            bd.setValue('handlegroup_id', obList.getValue('handlegroup_id'));
            $F.ajaxSync("SystemDo5", {
                token: proj.token,
                dbPool: "",
                xml: bd.getData()
            }, proj.digest);
        }

        this.removeOblistAppCall = function(guid){
            var delAPCall = new BoundData("oblist_appcall"	+ $E.staff.epidSql + (this.extTBName||''));
            delAPCall.deleteFlag = 1;
            delAPCall.KeyInfoEdit("oblist_guid");
            delAPCall.NewValueEdit("oblist_guid", guid);
            var mRs = $F.ajaxSync("SystemDo5",{
                token: proj.token,
                dbPool: '',
                xml : delAPCall.getData()
            }, proj.digest);
        }
    };

    return {
        getInstance : function(proj){
            return new Outbound(proj);
        },
        getOBUtil : function(proj){
            return new Util(proj);
        }
    };
});